@echo off
chcp 65001 >nul
title Python3 Auto Installer

echo ================================
echo    Python3 Auto Installer
echo ================================
echo.

:: Set Python version and download URL
set PYTHON_VERSION=3.11.9
set PYTHON_URL=https://registry.npmmirror.com/-/binary/python/%PYTHON_VERSION%/python-%PYTHON_VERSION%-amd64.exe
set DOWNLOAD_DIR=%TEMP%\python_install
set INSTALLER=python-%PYTHON_VERSION%-amd64.exe
set INSTALL_PATH=C:\Python311

echo [Step 1] Creating download directory...
if not exist "%DOWNLOAD_DIR%" mkdir "%DOWNLOAD_DIR%"

echo [Step 2] Downloading Python %PYTHON_VERSION%...
echo From: %PYTHON_URL%
echo.

:: Try different download methods
set DOWNLOAD_SUCCESS=0

:: Method 1: Using bitsadmin
echo Trying bitsadmin...
bitsadmin /transfer "PythonDownload" /download /priority normal "%PYTHON_URL%" "%DOWNLOAD_DIR%\%INSTALLER%"
if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1

:: Method 2: Using certutil if bitsadmin failed
if %DOWNLOAD_SUCCESS% == 0 (
    echo Trying certutil...
    certutil -urlcache -split -f "%PYTHON_URL%" "%DOWNLOAD_DIR%\%INSTALLER%"
    if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1
)

:: Method 3: Using PowerShell if available
if %DOWNLOAD_SUCCESS% == 0 (
    echo Trying PowerShell...
    powershell -Command "Invoke-WebRequest -Uri '%PYTHON_URL%' -OutFile '%DOWNLOAD_DIR%\%INSTALLER%'"
    if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1
)

if %DOWNLOAD_SUCCESS% == 0 (
    echo ERROR: Download failed! Please check your internet connection.
    pause
    exit /b 1
)

echo.
echo [Step 3] Download completed!
echo.

echo [Step 4] Installing Python %PYTHON_VERSION%...
echo Installation path: %INSTALL_PATH%
echo.

:: Silent installation
echo Installing, please wait...
start /wait "" "%DOWNLOAD_DIR%\%INSTALLER%" /quiet InstallAllUsers=1 PrependPath=1 TargetDir="%INSTALL_PATH%" Include_test=0

if %errorlevel% neq 0 (
    echo ERROR: Python installation failed!
    pause
    exit /b 1
)

echo [Step 5] Python installation completed!
echo.

echo [Step 6] Configuring pip mirror...
echo.

:: Create pip config directory
set PIP_DIR=%APPDATA%\pip
if not exist "%PIP_DIR%" mkdir "%PIP_DIR%"

:: Create pip configuration file with Chinese mirror
(
echo [global]
echo index-url = https://pypi.tuna.tsinghua.edu.cn/simple
echo extra-index-url = https://mirrors.aliyun.com/pypi/simple/
echo trusted-host = pypi.tuna.tsinghua.edu.cn
echo                mirrors.aliyun.com
echo timeout = 300
) > "%PIP_DIR%\pip.ini"

echo Pip configured with Tsinghua and Aliyun mirrors.
echo.

echo [Step 7] Verifying installation...
echo.

:: Check Python installation
python --version >nul 2>&1
if %errorlevel% == 0 (
    echo Python verification: SUCCESS
    python --version
) else (
    echo Python verification: FAILED
    echo Please restart your command prompt or system.
)

:: Check pip
pip --version >nul 2>&1
if %errorlevel% == 0 (
    echo Pip verification: SUCCESS
) else (
    echo Pip verification: FAILED
)

echo.
echo ================================
echo    Installation Summary
echo ================================
echo Python Version: %PYTHON_VERSION%
echo Install Path: %INSTALL_PATH%
echo Pip Config: %PIP_DIR%\pip.ini
echo.
echo Please restart command prompt to ensure
echo environment variables take effect.
echo.

:: Clean up
echo [Step 8] Cleaning up...
if exist "%DOWNLOAD_DIR%" (
    del "%DOWNLOAD_DIR%\%INSTALLER%" >nul 2>&1
    rmdir "%DOWNLOAD_DIR%" >nul 2>&1
)

echo.
echo Installation completed successfully!
pause